﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'proc_cm_SaveDailyWordStatus')
BEGIN
    PRINT 'Dropping Procedure proc_cm_SaveDailyWordStatus'
    DROP  Procedure  proc_cm_SaveDailyWordStatus
END
GO

PRINT 'Creating Procedure proc_cm_SaveDailyWordStatus'
GO

CREATE PROCEDURE [dbo].[proc_cm_SaveDailyWordStatus]
	@pDailywordStatusId uniqueidentifier
	,@pDailywordId uniqueidentifier
	,@pUserId uniqueidentifier
	,@pReadInd bit
	,@pReflectionTxt nvarchar(max) = null
	,@pSubmittedInd bit
	,@pSubmittedDttm datetime = null
	,@pScoreNum int = null
	,@pPerfectScoreNum int = null
AS
BEGIN
	SET NOCOUNT ON

	UPDATE
		[dbo].[tbl_dailyword_status]
	SET 
		[dailyword_id] = @pDailywordId
		,[UserId] = @pUserId
		,[read_ind] = @pReadInd
		,[reflection_txt] = @pReflectionTxt
		,[submitted_ind] = @pSubmittedInd
		,[submitted_dttm] = @pSubmittedDttm
		,[score_num] = @pScoreNum
		,[perfect_score_num] = @pPerfectScoreNum
	WHERE
		[dailyword_status_id] = @pDailywordStatusId

	IF @@ROWCOUNT = 0
	BEGIN
		INSERT INTO [dbo].[tbl_dailyword_status]
		(
			[dailyword_status_id]
			,[dailyword_id]
			,[UserId]
			,[read_ind]
			,[reflection_txt]
			,[submitted_ind]
			,[created_dttm]
			,[submitted_dttm]
			,[score_num]
			,[perfect_score_num]
		)
		VALUES
		(
			@pDailywordStatusId
			,@pDailywordId
			,@pUserId
			,@pReadInd
			,@pReflectionTxt
			,@pSubmittedInd
			,GETDATE()
			,@pSubmittedDttm
			,@pScoreNum
			,@pPerfectScoreNum
		)
	END
END
GO

GRANT EXEC ON dbo.proc_cm_SaveDailyWordStatus TO PUBLIC
GO
